//
//  ParserXML.m
//  XMLParser
//
//  Created by WSXiong on 2017/7/12.
//  Copyright © 2017年 CMCC_CMSZ. All rights reserved.
//

#import "ParserXML.h"

@interface ParserXML ()<NSXMLParserDelegate>{
    NSString *_keyName;
}

@property (nonatomic, strong) NSMutableDictionary *mutDict;

@end

@implementation ParserXML

- (NSMutableDictionary *)mutDict{
    if (!_mutDict) {
        _mutDict = [NSMutableDictionary dictionary];
    }
    return _mutDict;
}


- (void)createPayment:(NSString *)body andViewController:(UIViewController *)vc andAppUrlScheme:(NSString *)urlScheme withCompletion:(myBlock)completion{
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    parser.delegate = self;
    [parser parse];
    if (![_mutDict[@"ResultCode"] isEqualToString:@"014A17"] ) {
        
        return;
    }
    for (NSString *key in [_mutDict allKeys]) {
        if (![@[@"Parameters",@"PaymentType"] containsObject:key] ) {
            [_mutDict removeObjectForKey:key];
        }
    }
    NSLog(@"--%@",_mutDict);
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
    if ([elementName isEqualToString:@"Body"]) {
        return;
    }
    _keyName = elementName;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    [self.mutDict setValue:string forKey:_keyName];
}

@end
